<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                style="vertical-align:middle"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="tableData">
              <el-table-column type="index" label="序号" width="120" />
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template slot-scope="{row}">
                  <el-button size="small" type="success" @click="assignPerm(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="edit(row.id)">编辑</el-button>
                  <el-button size="small" type="danger">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                layout="sizes,prev, pager, next"
                :total="total"
                :page-size="params.pagesize"
                :page-sizes="[2,5,10]"
                :current-page="params.page"
                @current-change="changeCurrent"
                @size-change="changeSize"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>

      <el-dialog title="编辑弹层" :visible="showDialog" @close="btnCancel">
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="btnCancel">取消</el-button>
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <!-- :default-checked-keys="permIds" 勾选permIds数组中的数据 -->
      <!--check-strictly 父子间不互相关联 -->
      <el-dialog :visible="showPermDialog" title="分配权限" @close="btnPermCancel">
        <el-tree
          ref="assignPerm"
          :data="permList"
          :props="{label:'name'}"
          :show-checkbox="true"
          :check-strictly="true"
          :default-expand-all="true"
          :default-checked-keys="permIds"
          node-key="id"
        >
          <!-- <template #default="{row}">{{ row}}</template> -->
        </el-tree>
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button type="primary" size="small" @click="btnPermOK">确定</el-button>
            <el-button size="small" @click="btnPermCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getRoleList, getRoleDetail, editRole, assignPerm } from '@/api/setting'
import { getPermissionList } from '@/api/permisson'
import { listTree } from '@/utils'
export default {
  data() {
    return {
      showPermDialog: false,
      permList: [],
      permIds: [],
      roleId: null,
      tableData: [],
      total: 0,
      params: {
        pagesize: 2,
        page: 1
      },
      showDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '名称不能为空', trigger: 'blur' }]
      }

    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async  getRoleList() {
      const { total, rows } = await getRoleList(this.params)
      this.tableData = rows
      this.total = total
    },
    changeSize(size) {
      this.params.pagesize = size
      this.params.page = 1 // 改变当前条数 直接跳到第一页
      this.getRoleList()
    },
    changeCurrent(page) {
      this.params.page = page
      this.getRoleList()
    },
    async  edit(id) {
      this.showDialog = true
      this.roleForm = await getRoleDetail(id)
    },
    async btnOK() {
      await this.$refs.roleForm.validate()
      await editRole(this.roleForm)
      this.getRoleList()
      this.showDialog = false
      this.$message.success('编辑成功')
    },
    btnCancel() { // 只要弹窗关闭就会走 新增和修改都会
      // 1.关闭弹窗
      this.showDialog = false
      //   2.清空校验
      this.$refs.roleForm.clearValidate()
      // 3.重置表单数据
      this.roleForm = {
        name: '',
        description: ''
      }
    },
    // 分配权限
    async  assignPerm(id) {
      // 这里使用async await是为了解决上个界面遗留一闪而过的问题，会导致弹窗打开变慢，element有bug 重新打开页面就算permIds为空 页面还是上一次打开的遗留
      await this.getPermissionList()
      await this.getRoleDetail(id)
      this.showPermDialog = true
      this.roleId = id
    },
    async getPermissionList() {
      this.permList = listTree(await getPermissionList(), '0')
    },
    async getRoleDetail(id) {
      const { permIds } = await getRoleDetail(id)
      this.permIds = permIds
    },
    async  btnPermOK() {
      this.permIds = this.$refs.assignPerm.getCheckedKeys()
      await assignPerm({ id: this.roleId, permIds: this.permIds })
      this.showPermDialog = false
      this.$message.success('成功')
    },
    btnPermCancel() {
      // 1.关闭弹窗
      this.showPermDialog = false
      this.permIds = []
    }
  }
}
</script>

<style>
.el-icon-plus{
    font-size: 14px;
}
</style>
